package com.xiumu.common.minio;

import io.minio.*;
import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;

@Slf4j
public class MinioTemplate {

    private MinioClient minioClient;

    /**
     * 配置信息
     */
    private MinioProperties properties;

    public MinioTemplate() {
    }

    public MinioTemplate(MinioProperties properties) {
        this.properties = properties;
        minioClient = MinioClient.builder()
                .endpoint(properties.getHost())
                .credentials(properties.getAccessKey(), properties.getSecretKey())
                .build();
    }

    /**
     * 判断存储空间是否存在
     *
     * @param bucketName 存储空间名称
     * @return
     */
    public boolean exist(String bucketName) {
        try {
            return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        } catch (Exception e) {
            log.error("exist 查询存储空间失败", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建存储空间
     *
     * @param bucketName 存储空间名称
     * @return
     */
    public boolean makeBucket(String bucketName) {
        try {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            return true;
        } catch (Exception e) {
            log.error("创建存储空间失败", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 上传文件
     *
     * @param inputStream      输入流
     * @param originalFileName 文件名 | person.png
     */
    public String uploadFile(InputStream inputStream, Long fileSize, String originalFileName) {
        String key = System.currentTimeMillis() + originalFileName;
        PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                .bucket(properties.getDefaultBucketName())
                .object(key)
                .stream(inputStream, fileSize, -1)
                .build();
        try(inputStream) {
            minioClient.putObject(putObjectArgs);
            return key;
        } catch (Exception e) {
            log.error("uploadFile 上传文件失败", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取文件的访问链接
     *
     * @param bucket 存储空间
     * @param key    文件的 key
     */
    private String getFilePublicUrl(String bucket, String key) {
        return properties.getDomain() + bucket + "/" + key;
    }

    /**
     * 获取默认存储空间文件的访问链接
     *
     * @param key 文件的 key
     */
    public String getPublicUrlOfDefaultBucketFile(String key) {
        return properties.getDomain() + properties.getDefaultBucketName() + "/" + key;
    }

    /**
     * 删除默认存储空间的文件
     *
     * @param key 文件的 key
     */
    public void deleteFileOfDefaultBucketFile(String key) {
        log.warn("删除文件 {}", properties.getDefaultBucketName() + "/" + key);
        RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder()
                .bucket(properties.getDefaultBucketName())
                .object(key)
                .build();
        try {
            minioClient.removeObject(removeObjectArgs);
        } catch (Exception e) {
            log.error("删除文件失败", e);
            throw new RuntimeException(e);
        }
    }

}
